using System;
using System.Collections.Generic;

namespace com.adjust.sdk
{
	public class AdjustUtils
	{
		public static string KeyAdid = "adid";

		public static string KeyMessage = "message";

		public static string KeyNetwork = "network";

		public static string KeyAdgroup = "adgroup";

		public static string KeyCampaign = "campaign";

		public static string KeyCreative = "creative";

		public static string KeyWillRetry = "willRetry";

		public static string KeyTimestamp = "timestamp";

		public static string KeyCallbackId = "callbackId";

		public static string KeyEventToken = "eventToken";

		public static string KeyClickLabel = "clickLabel";

		public static string KeyTrackerName = "trackerName";

		public static string KeyTrackerToken = "trackerToken";

		public static string KeyJsonResponse = "jsonResponse";

		public static string KeyCostType = "costType";

		public static string KeyCostAmount = "costAmount";

		public static string KeyCostCurrency = "costCurrency";

		public static string KeyFbInstallReferrer = "fbInstallReferrer";

		public static string KeySkadConversionValue = "fineValue";

		public static string KeySkadCoarseValue = "coarseValue";

		public static string KeySkadLockWindow = "lockWindow";

		public static string KeyTestOptionsBaseUrl = "baseUrl";

		public static string KeyTestOptionsGdprUrl = "gdprUrl";

		public static string KeyTestOptionsSubscriptionUrl = "subscriptionUrl";

		public static string KeyTestOptionsExtraPath = "extraPath";

		public static string KeyTestOptionsBasePath = "basePath";

		public static string KeyTestOptionsGdprPath = "gdprPath";

		public static string KeyTestOptionsDeleteState = "deleteState";

		public static string KeyTestOptionsUseTestConnectionOptions = "useTestConnectionOptions";

		public static string KeyTestOptionsTimerIntervalInMilliseconds = "timerIntervalInMilliseconds";

		public static string KeyTestOptionsTimerStartInMilliseconds = "timerStartInMilliseconds";

		public static string KeyTestOptionsSessionIntervalInMilliseconds = "sessionIntervalInMilliseconds";

		public static string KeyTestOptionsSubsessionIntervalInMilliseconds = "subsessionIntervalInMilliseconds";

		public static string KeyTestOptionsTeardown = "teardown";

		public static string KeyTestOptionsNoBackoffWait = "noBackoffWait";

		public static string KeyTestOptionsiAdFrameworkEnabled = "iAdFrameworkEnabled";

		public static string KeyTestOptionsAdServicesFrameworkEnabled = "adServicesFrameworkEnabled";

		public static int ConvertLogLevel(AdjustLogLevel? logLevel)
		{
			if (!logLevel.HasValue)
			{
				return -1;
			}
			return (int)logLevel.Value;
		}

		public static int ConvertBool(bool? value)
		{
			if (!value.HasValue)
			{
				return -1;
			}
			if (value.Value)
			{
				return 1;
			}
			return 0;
		}

		public static double ConvertDouble(double? value)
		{
			if (!value.HasValue)
			{
				return -1.0;
			}
			return value.Value;
		}

		public static int ConvertInt(int? value)
		{
			if (!value.HasValue)
			{
				return -1;
			}
			return value.Value;
		}

		public static long ConvertLong(long? value)
		{
			if (!value.HasValue)
			{
				return -1L;
			}
			return value.Value;
		}

		public static string ConvertListToJson(List<string> list)
		{
			if (list == null)
			{
				return null;
			}
			if (list.Count % 2 != 0)
			{
				return null;
			}
			List<string> list2 = new List<string>();
			for (int i = 0; i < list.Count; i += 2)
			{
				string text = list[i];
				string text2 = list[i + 1];
				if (text != null && text2 != null)
				{
					list2.Add(text);
					list2.Add(text2);
				}
			}
			JSONArray jSONArray = new JSONArray();
			foreach (string item in list2)
			{
				jSONArray.Add(new JSONData(item));
			}
			return jSONArray.ToString();
		}

		public static string GetJsonResponseCompact(Dictionary<string, object> dictionary)
		{
			string text = "";
			if (dictionary == null)
			{
				return text;
			}
			int num = 0;
			text += "{";
			foreach (KeyValuePair<string, object> item in dictionary)
			{
				if (item.Value is string text2)
				{
					if (++num > 1)
					{
						text += ",";
					}
					text = ((!text2.StartsWith("{") || !text2.EndsWith("}")) ? (text + "\"" + item.Key + "\":\"" + text2 + "\"") : (text + "\"" + item.Key + "\":" + text2));
				}
				else
				{
					Dictionary<string, object> dictionary2 = item.Value as Dictionary<string, object>;
					if (++num > 1)
					{
						text += ",";
					}
					text = text + "\"" + item.Key + "\":";
					text += GetJsonResponseCompact(dictionary2);
				}
			}
			return text + "}";
		}

		public static string GetJsonString(JSONNode node, string key)
		{
			if (node == null)
			{
				return null;
			}
			JSONData jSONData = node[key] as JSONData;
			if (jSONData == null)
			{
				return null;
			}
			if ((JSONNode)jSONData == (object)"")
			{
				return null;
			}
			return jSONData.Value;
		}

		public static void WriteJsonResponseDictionary(JSONClass jsonObject, Dictionary<string, object> output)
		{
			foreach (KeyValuePair<string, JSONNode> item in jsonObject)
			{
				JSONClass asObject = item.Value.AsObject;
				string key = item.Key;
				if (asObject == null)
				{
					string value = item.Value.Value;
					output.Add(key, value);
				}
				else
				{
					Dictionary<string, object> dictionary = new Dictionary<string, object>();
					output.Add(key, dictionary);
					WriteJsonResponseDictionary(asObject, dictionary);
				}
			}
		}

		public static string TryGetValue(Dictionary<string, string> dictionary, string key)
		{
			if (dictionary.TryGetValue(key, out var value))
			{
				if (value == "")
				{
					return null;
				}
				return value;
			}
			return null;
		}

		public static int GetSkad4ConversionValue(string conversionValueUpdate)
		{
			JSONNode jSONNode = JSON.Parse(conversionValueUpdate);
			if (jSONNode == null)
			{
				return -1;
			}
			string jsonString = GetJsonString(jSONNode, KeySkadConversionValue);
			int result = 0;
			if (int.TryParse(jsonString, out result))
			{
				return result;
			}
			return -1;
		}

		public static string GetSkad4CoarseValue(string conversionValueUpdate)
		{
			JSONNode jSONNode = JSON.Parse(conversionValueUpdate);
			if (jSONNode == null)
			{
				return null;
			}
			return GetJsonString(jSONNode, KeySkadCoarseValue);
		}

		public static bool GetSkad4LockWindow(string conversionValueUpdate)
		{
			JSONNode jSONNode = JSON.Parse(conversionValueUpdate);
			if (jSONNode == null)
			{
				return false;
			}
			return Convert.ToBoolean(GetJsonString(jSONNode, KeySkadLockWindow));
		}
	}
}
